/**
 * 
 */
package persistence;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.Session;

import jpa_objects.Event;
import jpa_objects.Feedback;
import jpa_objects.Message;
import jpa_objects.User;
import jpa_objects.User_report;

/**
 * @author Alejandro Casas
 *
 */
public class DBCommonAlter {


	public static final String SAVE_USER="SAVE";
	public static final String UPDATE_USER="UPDATE";
	public static final String SAVE_EVENT="SAVE_EVENT";
	public static final String UPDATE_EVENT="UPDATE_EVENT";

	public DBCommonAlter() {

	}

	public boolean saveEvent(Event e,String action) {
		Session session=null;
		if(DBConnector.sf!=null){
			session = DBConnector.sf.getCurrentSession();

		}
		if(session==null||!session.isOpen())
		{
			session =DBConnector.openSession();
		}
		session.beginTransaction();
		if(action.equals(DBCommonAlter.UPDATE_EVENT))
		{
			session.update(e);
		}
		else if(action.equals(DBCommonAlter.SAVE_EVENT))
		{
			session.save(e);
		}
		session.getTransaction().commit();
		return true;

	}



	public void saveUser(User u, String action) {
		Session session=null;
		if(DBConnector.sf!=null){
			session = DBConnector.sf.getCurrentSession();

		}
		if(session==null||!session.isOpen())
		{
			session =DBConnector.openSession();
		}
		session.beginTransaction();
		if(action.equals(SAVE_USER))
		{
			session.save(u);
		}
		else if(action.equals(UPDATE_USER))
		{
			session.update(u);
		}
		session.getTransaction().commit();

	}

	public boolean saveReport(User_report r) {
		Session session=null;
		if(DBConnector.sf!=null){
			session = DBConnector.sf.getCurrentSession();

		}
		if(session==null||!session.isOpen())
		{
			session =DBConnector.openSession();
		}
		session.beginTransaction();
		session.save(r);
		session.getTransaction().commit();
		return true;
	}

	public boolean saveFeedback(Feedback f) {
		Session session=null;
		if(DBConnector.sf!=null){
			session = DBConnector.sf.getCurrentSession();

		}
		if(session==null||!session.isOpen())
		{
			session =DBConnector.openSession();
		}
		session.beginTransaction();
		session.save(f);
		session.getTransaction().commit();
		return true;
		
	}

}
